package com.ruoyi.comparison.utils;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

@Slf4j
@Configuration
public class KeyHandleUtil {
    private static final String path = "副本";
    private static final String pdf = "pdf";
    public Set<String> PDFByKeyWord(String filePath, Set<String> keys) {
//        File file = new File(filePath);
//
//        String name = file.getName();
//        int index = filePath.lastIndexOf(name);
//        String substring = filePath.substring(0, index);
//        String s = path + name;
//        FileInputStream fis = new FileInputStream(file);
//        WordUtil.clearHeaderAndFooter(fis,substring+s);
//        int i1 = s.lastIndexOf(".");
//        String s1 =substring +"/pdf"+ s.substring(0, i1+1) + pdf;
//        AsposeUtil.wordToPdf(s,s1);
        Set<String> set=new HashSet<>();
        try {
            InputStream inputStream = Files.newInputStream(Paths.get(filePath));
            PdfReader reader = new PdfReader(inputStream);
            for (String key : keys) {
                List<String> list = new ArrayList<>();
                for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                    String string = PdfTextExtractor.getTextFromPage(reader, i);
                    String textPage = string.replaceAll("\\s*", "");
                    if (textPage.length()<5){
                        continue;
                    }
                    if (textPage.contains(key)){
                        set.add(key);
                        break;
                    }else {
                        String s2 = pageNum(key, textPage);
                        list.add(s2);
                    }
                }
                String s = list.stream().max(Comparator.comparingInt(String::length)).orElse("");
                set.add(s);
            }
            inputStream.close();
            reader.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        List<String> list = new ArrayList<>(set);
        for (int i = 0; i < list.size(); i++) {
            for (String s : list) {
                if (s.contains(list.get(i)) && (!s.equals(list.get(i)))) {
                    set.remove(list.get(i));
                }
            }
        }
        return set;
    }
    private String pageNum(String key, String textPage){
        int length = key.length();
        int a = 0;
        String substring = key.substring(0,length - a);
        for (int i = 0; i < length-5; i++) {
            if (textPage.contains(substring)){
                int length1 = substring.length();
                if (StringUtils.isNumeric(substring.substring(length1 - 1))){
                    return substring.substring(0, length1 -1);
                }else if (StringUtils.isNumeric(substring.substring(length1 - 2))){
                    return substring.substring(0, length1 -2);
                }else {
                    return substring.substring(0, length1);
                }

            }else {
                a++;
                substring = key.substring(0,length - a);
            }
        }
        return "";
    }
}
